package org.example.mallchat.dao;

import org.example.mallchat.domain.entity.UserBackpack;
import org.example.mallchat.mapper.UserBackpackMapper;
import org.example.mallchat.service.iservice.IUserBackpackService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 用户背包表 服务实现类
 * </p>
 *
 * @author 
 * @since 2024-12-22
 */
@Service
public class UserBackpackDao extends ServiceImpl<UserBackpackMapper, UserBackpack> implements IUserBackpackService {

    /**
     * 获取用户背包中改名卡的数量 且没使用过的
     * @param uid
     * @param id
     * @return
     */
    @Override
    public int getRenameCardCount(String uid, Long id) {
        return lambdaQuery().eq(UserBackpack::getUid, uid)
                .eq(UserBackpack::getStatus,0)
                .eq(UserBackpack::getItemId, id).count();
    }

    /**
     * 减少改名卡的数量
     * @param uid
     * @param id
     */
    @Override
    public void reduceCardCount(String uid, Long id) {
        UserBackpack one = lambdaQuery().eq(UserBackpack::getUid, uid)
                .eq(UserBackpack::getItemId, id)
                .eq(UserBackpack::getStatus, 0).one();
        if (one!= null) {
            lambdaUpdate().set(UserBackpack::getStatus, 1).eq(UserBackpack::getId, one.getId());
        }
    }


    /**
     * 获取用户背包中存在的物品
     * @param uid
     * @param itemIds
     * @return
     */
    @Override
    public List<UserBackpack> getBadgeList(String uid,List<Long> itemIds) {
        return lambdaQuery().eq(UserBackpack::getUid, uid)
                .in(UserBackpack::getItemId, itemIds).list();
    }

    /**
     * 穿戴徽章
     * @param uid
     * @param wearingItemId
     */
    @Override
    public void wearBadge(String uid, Long wearingItemId) {
        this.lambdaUpdate().set(UserBackpack::getStatus, 1)
                .eq(UserBackpack::getUid, uid)
                .eq(UserBackpack::getItemId, wearingItemId)
                .update();
    }

    /**
     * 根据幂等号获取背包信息
     * @param idempotentKey
     * @return
     */
    @Override
    public UserBackpack getBackpackByIdempotent(String idempotentKey) {
        return lambdaQuery().eq(UserBackpack::getIdempotent, idempotentKey).one();
    }
}
